#include "configs.h"
#include "switch.h"
#include "timer.h"
#include "sys.h"
#include "common.h"
#include "oled.h"
STRConfigRegType g_config_stRegs;
#define CONFIG_PARAM_NUM 8
#if(0)
const STRConfigDefType configs_def_table[CONFIG_PARAM_NUM] = {
    {.addvalue =1, .decvalue=1, .maxvalue=1, .minvalue=0, .displayrow=0, .defaultvalue=0},
    {.addvalue =1, .decvalue=1, .maxvalue=10000, .minvalue=0, .displayrow=1, .defaultvalue=35},
    {.addvalue =20, .decvalue=20, .maxvalue=10000, .minvalue=0, .displayrow=2, .defaultvalue=70},
    {.addvalue =20, .decvalue=20, .maxvalue=10000, .minvalue=0, .displayrow=3, .defaultvalue=1000},
    {.addvalue =50, .decvalue=50, .maxvalue=10000, .minvalue=0, .displayrow=4, .defaultvalue=500},
    {.addvalue =50, .decvalue=50, .maxvalue=10000, .minvalue=0, .displayrow=5, .defaultvalue=100},
    {.addvalue =50, .decvalue=50, .maxvalue=10000, .minvalue=0, .displayrow=6, .defaultvalue=50},
    {.addvalue =50, .decvalue=50, .maxvalue=10000, .minvalue=0, .displayrow=7, .defaultvalue=500},

};
#else
const STRConfigDefType configs_def_table[CONFIG_PARAM_NUM] = {
    { 1, 1, 1, 0, 0, 0},
    {1, 1, 10000, 0, 1, 35},
    {20, 20, 10000, 0, 2, 70},
    {20, 20, 10000, 0, 3, 1000},
    {50, 50, 10000, 0, 4, 500},
    {50, 50, 10000, 0, 5, 100},
    {50, 50, 10000, 0, 6, 50},
    {50, 50, 10000, 0, 7, 500},

};
#endif

void fnConfigs_Init(void);
void fnConfigs_RealTime(void);
void fnConfigs_Init(void) {
	U8 i;
    memset(&g_config_stRegs, 0, sizeof(g_config_stRegs));
	for(i = 0; i< CONFIG_PARAM_NUM; i++){
		g_config_stRegs.configvalue[i] = configs_def_table[i].defaultvalue;
	}
}
void fnConfigs_RealTime(void) {
    U8 buff[64];
    U8 configitem;
    U16 configvalue;
    U8 i;
    if(SW_KEY_PRESS(SW_OFFSET_ENTERCONFIG)) {
		OLED_Clear();
        g_config_stRegs.configitem ++;
        if(g_config_stRegs.configitem  > CONFIG_PARAM_NUM) {
            g_config_stRegs.configitem = 0;
        }
    }


    if(g_config_stRegs.configitem > 0) {
        configitem = g_config_stRegs.configitem - 1;
        if(configitem < CONFIG_PARAM_NUM) {
            configvalue = g_config_stRegs.configvalue[configitem];
            if(SW_KEY_PRESS(SW_OFFSET_ADD)) {
                configvalue += configs_def_table[configitem].addvalue;
                if(configvalue > configs_def_table[configitem].maxvalue) {
                    configvalue = configs_def_table[configitem].maxvalue;
                }
                g_config_stRegs.configvalue[configitem] = configvalue;
            }
            if(SW_KEY_PRESS(SW_OFFSET_DEC)) {
                if(configvalue < (configs_def_table[configitem].minvalue + configs_def_table[configitem].decvalue)) {
                    configvalue = configs_def_table[configitem].minvalue ;
                } else {
                    configvalue -= configs_def_table[configitem].decvalue;
                }
                g_config_stRegs.configvalue[configitem] = configvalue;
            }
        }
        if(SW_KEY_PRESS(SW_OFFSET_EXITCONFIG)) {
            g_config_stRegs.configitem = 0;
            OLED_Clear();
        }
    } else {
        if(SW_KEY_PRESS(SW_OFFSET_EXITCONFIG)) {
			OLED_Clear();
            g_config_stRegs.displaytype ++;
            if(g_config_stRegs.displaytype > 1) {
                g_config_stRegs.displaytype = 0;
            }

        }
    }
    if(g_tm_stTimerFlag.Bits.bTimer100ms) {
        if(g_config_stRegs.configitem > 0) {
            sprintf((char *)buff,"Working md:%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_MODE]);
            OLED_ShowString(8,0,buff,12);
            sprintf((char *)buff,"A_Temp    :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_TEMP]);
            OLED_ShowString(8,1,buff,12);
            sprintf((char *)buff,"A_Hum     :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_HUM]);
            OLED_ShowString(8,2,buff,12);
            sprintf((char *)buff,"A_pre     :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_PRE]);
            OLED_ShowString(8,4,buff,12);
            sprintf((char *)buff,"A_gz_val  :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_GZ]);
            OLED_ShowString(8,3,buff,12);
            sprintf((char *)buff,"A_m2_val  :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M2]);
            OLED_ShowString(8,5,buff,12);
            sprintf((char *)buff,"A_m7_val  :%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M7]);
            OLED_ShowString(8,6,buff,12);
            sprintf((char *)buff,"A_m135_val:%4d",g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M135]);
            for(i = 0; i < 8; i++) {
                if((i + 1) == g_config_stRegs.configitem) {
                    OLED_ShowString(0,i,(U8 *)">",12);
                } else {
                    OLED_ShowString(0,i,(U8 *)" ",12);
                }
            }
        } else {
            if(g_config_stRegs.displaytype == 0){
                OLED_ShowCHinese(0,0,0);
                OLED_ShowCHinese(16,0,2);
                sprintf((char*)buff,":%2dC", g_config_stRegs.displaydata->temp);
                OLED_ShowString(32,0,buff,16);	//显示温度
                OLED_ShowCHinese(64,0,1);
                OLED_ShowCHinese(82,0,2);
                sprintf((char*)buff,":%2d%%", g_config_stRegs.displaydata->himi);
                OLED_ShowString(96,0,buff,16);//显示湿度

                OLED_ShowCHinese(0,2,17);
                OLED_ShowCHinese(16,2,18);
                OLED_ShowCHinese(32,2,19);
                OLED_ShowCHinese(48,2,20);
                sprintf((char*)buff,":%4dppm", g_config_stRegs.displaydata->CO);
                OLED_ShowString(64,2,buff,16);//显示M7数据

                OLED_ShowCHinese(0,4,9);
                OLED_ShowCHinese(16,4,10);
                OLED_ShowCHinese(32,4,11);
                OLED_ShowCHinese(48,4,12);
                sprintf((char*)buff,":%4dppm", g_config_stRegs.displaydata->airquality);
                OLED_ShowString(64,4,buff,16);//显示M135数据

                OLED_ShowCHinese(0,6,5);
                OLED_ShowCHinese(16,6,6);
                OLED_ShowCHinese(32,6,7);
                OLED_ShowCHinese(48,6,8);
                sprintf((char*)buff,":%4dLux", g_config_stRegs.displaydata->gz);
                OLED_ShowString(64,6,buff,16);//显示光照强度
        }
		else
        {
            OLED_ShowCHinese(0,0,13);
            OLED_ShowCHinese(16,0,14);
            OLED_ShowCHinese(32,0,15);
            OLED_ShowCHinese(48,0,16);
            sprintf((char*)buff,":%4dppm", g_config_stRegs.displaydata->yw);
            OLED_ShowString(64,0,buff,16);//显示M2数据

            OLED_ShowCHinese(0,2,17);
            OLED_ShowCHinese(16,2,18);
            OLED_ShowCHinese(32,2,19);
            OLED_ShowCHinese(48,2,20);
            sprintf((char*)buff,":%4dppm", g_config_stRegs.displaydata->CO);
            OLED_ShowString(64,2,buff,16);//显示M7数据

            OLED_ShowCHinese(0,4,9);
            OLED_ShowCHinese(16,4,10);
            OLED_ShowCHinese(32,4,11);
            OLED_ShowCHinese(48,4,12);
            sprintf((char*)buff,":%4dppm", g_config_stRegs.displaydata->airquality);
            OLED_ShowString(64,4,buff,16);//显示M135数据

            OLED_ShowCHinese(0,6,5);
            OLED_ShowCHinese(16,6,6);
            OLED_ShowCHinese(32,6,7);
            OLED_ShowCHinese(48,6,8);
            sprintf((char*)buff,":%4dLux", g_config_stRegs.displaydata->gz);
            OLED_ShowString(64,6,buff,16);//显示光照强度
        }
    }
}
}
U8 fnConfigs_InConfig(void) {
    U8 res = FALSE;
    if(g_config_stRegs.configitem > 0) {
        res = TRUE;
    }
		return(res);
}
void fnConfigs_ConfigDisplayData(STRAppAttrType *displaydata){
	g_config_stRegs.displaydata = displaydata;
}